﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core;

namespace BSCMFF.Web.Pages.Evaluation_Setup
{
    /// <summary>
    /// Evaluates employees.
    /// </summary>
    /// <remarks></remarks>
    public partial class EvaluateEmployees : MultiLanguagePage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();

            RadGridInterviewNeeded.Culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            RadGridEvaluatedEmployees.Culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            RadGridNotEvaluatedEmployees.Culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            
            BSCEntities context = new BSCEntities();
            
            if (!IsPostBack && Request.QueryString["EvaluationEvaluatorEmployee"] != null)
            {
                int eeeID = Int32.Parse(Request.QueryString["EvaluationEvaluatorEmployee"]);
                Evaluation_Evaluator_Employee eee = (from ee in context.Evaluation_Evaluator_Employee
                                                     where ee.ID == eeeID
                                                     select ee).SingleOrDefault();

                EvalSelection.PublicSelectedValue = eee.Evaluation_FK; ;
            }

            if (EvalSelection.PublicSelectedValue != 0)
            {
                PanelGrids.Visible = true;
                int currUserId = (int)Session["loggedUserID"]; //(int)Session["userID"];

                FillInterviewNeededGrid(EvalSelection.PublicSelectedValue, currUserId);
                FillNotEvaluatedEmployeesGrid(EvalSelection.PublicSelectedValue, currUserId);

                FillEvaluatedEmployeesGrid(EvalSelection.PublicSelectedValue, currUserId);
            }                  
        }

        /// <summary>
        /// Fills the interview needed grid.
        /// </summary>
        /// <param name="evalID">The eval ID.</param>
        /// <param name="evaluatorID">The evaluator ID.</param>
        /// <remarks></remarks>
        private void FillInterviewNeededGrid(int evalID, int evaluatorID)
        {
            BSCEntities context = new BSCEntities();

            List<Evaluation_Evaluator_Employee> eees = (from eee in context.Evaluation_Evaluator_Employee.Include("User.SBU")
                                           where eee.Evaluation_FK == evalID && eee.Filled == false && eee.Evaluator_FK == evaluatorID
                                           select eee).ToList();

            RadGridInterviewNeeded.DataSource = eees;
            RadGridInterviewNeeded.DataBind();
        }

        /// <summary>
        /// Fills the not evaluated employees grid.
        /// </summary>
        /// <param name="evalID">The eval ID.</param>
        /// <param name="evaluatorID">The evaluator ID.</param>
        /// <remarks></remarks>
        private void FillNotEvaluatedEmployeesGrid(int evalID, int evaluatorID)
        {
            BSCEntities context = new BSCEntities();

            List<Evaluation_Evaluator_Employee> eees = (from eee in context.Evaluation_Evaluator_Employee.Include("User.SBU")
                                                        where eee.Evaluation_FK == evalID &&
                                                         eee.Filled == true &&
                                                         eee.Evaluated == false &&
                                                         eee.Evaluator_FK == evaluatorID
                                                        select eee).ToList();

            RadGridNotEvaluatedEmployees.DataSource = eees;
            RadGridNotEvaluatedEmployees.DataBind();
        }

        /// <summary>
        /// Fills the evaluated employees grid.
        /// </summary>
        /// <param name="evalID">The eval ID.</param>
        /// <param name="evaluatorID">The evaluator ID.</param>
        /// <remarks></remarks>
        private void FillEvaluatedEmployeesGrid(int evalID, int evaluatorID)
        {
            BSCEntities context = new BSCEntities();

            List<Evaluation_Evaluator_Employee> eees = (from eee in context.Evaluation_Evaluator_Employee.Include("User.SBU")
                                                        where eee.Evaluation_FK == evalID && eee.Evaluated == true && eee.Evaluator_FK == evaluatorID
                                                        select eee).ToList();

            RadGridEvaluatedEmployees.DataSource = eees;
            RadGridEvaluatedEmployees.DataBind();
        }

        /// <summary>
        /// Evaluations the selected index changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void EvaluationSelectedIndexChanged(object sender, EventArgs e)
        {
            RadComboBoxSelectedIndexChangedEventArgs re = (RadComboBoxSelectedIndexChangedEventArgs)e;
            int evalID = Int32.Parse(re.Value);
            int currUserId = (int)Session["loggedUserID"]; //(int)Session["userID"];

            FillEvaluatedEmployeesGrid(evalID, currUserId);
            FillNotEvaluatedEmployeesGrid(evalID, currUserId);
        }

        /// <summary>
        /// Handles the ItemCommand event of the RadGridInterviewNeeded control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.GridCommandEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadGridInterviewNeeded_ItemCommand(object sender, GridCommandEventArgs e)
        {
            int eeeid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());

            if (e.CommandName == "Delete")
            {
                EvaluationManager em = new EvaluationManager();
                Response.Redirect(Request.Url.AbsolutePath);
            }
            else
            {
                Response.Redirect("EvaluationFillingMode.aspx?Step=Interview&Mode=" + e.CommandName + "&EvaluationEvaluatorEmployee=" + eeeid);
            }
        }

        /// <summary>
        /// Handles the ItemCommand event of the RadGridEvaluatedEmployees control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.GridCommandEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadGridEvaluatedEmployees_ItemCommand(object sender, GridCommandEventArgs e)
        {
            int eeeid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());

            if (e.CommandName == "Delete")
            {
                EvaluationManager em = new EvaluationManager();
                em.DeleteEvaluationEvaluatorEmployee(eeeid);
                Response.Redirect(Request.Url.AbsolutePath);
            }
            else
            {
                Response.Redirect("EvaluationFillingMode.aspx?Mode=" + e.CommandName + "&EvaluationEvaluatorEmployee=" + eeeid);
            }
        }

        /// <summary>
        /// Handles the ItemCommand event of the RadGridNotEvaluatedEmployees control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.GridCommandEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadGridNotEvaluatedEmployees_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int eeeid = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString());
                Response.Redirect("EvaluationFillingMode.aspx?EvaluationEvaluatorEmployee=" + eeeid);
            }
        }

        /// <summary>
        /// Handles the ItemDataBound event of the RadGridEvaluatedEmployees control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.GridItemEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadGridEvaluatedEmployees_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (Request.QueryString["EvaluationEvaluatorEmployee"] != null)
            {
                int eeeID = Convert.ToInt32(Request.QueryString["EvaluationEvaluatorEmployee"]);

                if (e.Item is GridDataItem)
                {
                    GridDataItem dataItem = e.Item as GridDataItem;
                    int fieldValue = int.Parse(dataItem["ID"].Text);
                    if (eeeID == fieldValue)
                    {
                        e.Item.Selected = true;
                    }
                }
            }
        }

        /// <summary>
        /// Checks the session.
        /// </summary>
        /// <remarks></remarks>
        private void CheckSession()
        {
            if (Session["loggedUserID"] == null)
            {
                string login = HttpContext.Current.User.Identity.Name;

                BSCEntities context = new BSCEntities();

                User user = (from u in context.Users
                             where u.Login == login
                             select u).SingleOrDefault();

                if (user != null)
                {
                    Session.Add("loggedUserID", user.ID);
                }
            }
        }
    }
}